<?php
// +----------------------------------------------------------------------
// | Author: wyd
// +----------------------------------------------------------------------
// | Date: 2025/7/13
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

declare(strict_types=1);


namespace My\core\http;


class Request
{
    // 静态实例缓存
    protected static $instance;

    protected $request;
    protected $get;
    protected $post;
    protected $files;
    protected $cookie;
    protected $server;
    protected $rawContent;

    public function __construct(\Swoole\Http\Request $request)
    {
        $this->request = $request;
        $this->get = $request->get ?? [];
        $this->post = $request->post ?? [];
        $this->files = $request->files ?? [];
        $this->cookie = $request->cookie ?? [];
        $this->server = $request->server ?? [];
        $this->rawContent = $request->rawContent();  //获取原始数据
    }

    /**
     * 封装swoole的request
     * @param \Swoole\Http\Request $request
     * @return self
     */
    static public function init(\Swoole\Http\Request $request)
    {
        if ($request !== null) {
            self::$instance = new self($request);
        }
        if (!self::$instance) {
            throw new \RuntimeException('Request instance not initialized');
        }
        return self::$instance;
    }


    // 获取GET参数
    public function get($key = null, $default = null)
    {
        if ($key === null) {
            return $this->get;
        }
        return $this->get[$key] ?? $default;
    }

    // 获取POST参数
    public function post($key = null, $default = null)
    {
        if ($key === null) {
            return $this->post;
        }
        return $this->post[$key] ?? $default;
    }


    // 获取JSON输入
    public function json($key = null, $default = null)
    {
        $data = json_decode($this->rawContent, true);
        if ($key === null) {
            return $data ?? [];
        }
        return $data[$key] ?? $default;
    }

    // 获取上传文件
    public function file($key = null)
    {
        if ($key === null) {
            return $this->files;
        }
        return $this->files[$key] ?? null;
    }

    // 获取Cookie
    public function cookie($key = null, $default = null)
    {
        if ($key === null) {
            return $this->cookie;
        }
        return $this->cookie[$key] ?? $default;
    }

    // 获取请求头
    public function header($key = null, $default = null)
    {
        if ($key === null) {
            return $this->swooleRequest->header;
        }
        return $this->swooleRequest->header[strtolower($key)] ?? $default;
    }

    // 获取请求方法
    public function method()
    {
        return $this->server['request_method'] ?? 'GET';
    }

    // 获取请求URI
    public function uri()
    {
        return $this->server['request_uri'] ?? '/';
    }

    // 获取路径(不含查询字符串)
    public function path()
    {
        return strtok($this->uri(), '?');
    }

    // 获取完整URL
    public function url()
    {
        $scheme = $this->header('x-forwarded-proto') ??
            ($this->server['https'] ?? 'off') === 'on' ? 'https' : 'http';
        $host = $this->header('host') ?? 'localhost';
        return $scheme . '://' . $host . $this->uri();
    }

    // 获取原始请求内容
    public function rawContent()
    {
        return $this->rawContent;
    }

    // 获取Swoole原始请求对象
    public function getSwooleRequest()
    {
        return $this->swooleRequest;
    }
}